<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../res/css/common/font-awesome.min.css" media="all">
		<link rel="stylesheet" href="../../res/layui-config/css/app.css" media="all">
		<title>采购订单</title>
    </script>
	</head>
	
	<script type="text/html" id="input">
	<div class="layui-inline">
		<input class="layui-input" maxlength="5" style="border:none;padding-bottom:8px;" name="matAmount"  autocomplete="off"  value="{{d.matAmount}}" id="number{{d.matId}}" onkeyup="Pressdown(this);">
	</div>
	</script>
	
	<script type="text/html" id="inputss">
	<div class="layui-inline">
		<input class="layui-input layui-disabled"  readonly="readonly"  value="0" maxlength="9" style="border:none;padding-bottom:8px;" name="bdetTotal" id="price{{d.matId}}" autocomplete="off"">
	</div>
	</script>
	
	<body>
		
		<div class="kit-table">
			<div class="layui-card-header layui-form" style="padding: 8px 0px;">
				<!-- 搜索扩展工具条 -->
				<form class="layui-form" lay-filter="kit-search-form">
					<div class="layui-form-item">
						<div style="width: 244px;margin-left: 15px;">
							<select name="provId" lay-filter="biaodan" id="biaodan">
								<option value="">供应商</option>
							<select>
						</div>
						<div class="kit-search-btns">
						<a class="layui-btn layui-btn-sm" id="search" style="margin-left: 550px;margin-top: -78px;"><i class="layui-icon"></i> 搜索</a>
		                <a class="layui-btn layui-btn-sm" id="add" style="margin-left: 10px;margin-top: -78px;"><i class="layui-icon"></i> 增加</a>
		           		</div>
					</div>
				    <div class="layui-inline" style="margin-top: -202px;margin-left: 260px;">
      				<label class="layui-form-label">日期范围</label>
      					<div class="layui-input-inline">
        				<input type="text" class="layui-input" id="buyTime" placeholder=" - ">
      					</div>
    				</div>
				</form>
			</div>
		    
		    
			<!-- 操作按钮 -->
		    <div class="kit-table-body">
		        <table id="demo" lay-filter="demo"></table>
		        <script type="text/html" id="barDemo">
		            <button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
		            <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
					<button class="layui-btn layui-btn-xs" lay-event="detail">采购明细</button>
					<button class="layui-btn layui-btn-xs" lay-event="confirms">确认采购</button>
					<button class="layui-btn layui-btn-xs" lay-event="submission">提交质检</button>
		        </script>
		    </div>
		</div>
		
		
		<script src="../../res/js/common/jquery-1.11.2.min.js"></script>
		<script>
			//文本框只能输入金额
		    function amount(th){
		        var regStrs = [
		            ['^0(\\d+)$', '$1'], //禁止录入整数部分两位以上，但首位为0
		            ['[^\\d\\.]+$', ''], //禁止录入任何非数字和点
		            ['\\.(\\d?)\\.+', '.$1'], //禁止录入两个以上的点
		            ['^(\\d+\\.\\d{2}).+', '$1'] //禁止录入小数点后两位以上
		        ];
		        for(i=0; i<regStrs.length; i++){
		            var reg = new RegExp(regStrs[i][0]);
		            th.value = th.value.replace(reg, regStrs[i][1]);
		        }
		    }
			//搜索框同步输入
			function lockInput(obj,id){
				$("#"+id).val($(obj).val());
			}
			
		
		</script>
		<script src="../../res/layui/layui.all.js"></script>
		<script>
			window.windata=null;
			//显示表格数据
		    layui.use(['table','upload'], function() {
		        var table = layui.table,
		            $ = layui.jquery,
		            layer = layui.layer,
		            form = layui.form,
		            laytpl = layui.laytpl;
		        var tableIns = table.render({
		            elem: '#demo',
		            height: 'full-60', //容器高度
		            url: '../../buyht/showList.action',
		            page: true,
		            id: 'demo',
		            cols: [
		                [{checkbox: true,fixed: true},
		                {field: 'buyId',title: 'ID',width: 120},
		                {field: 'provName',title: '供应商',width: 120},
		                {field: 'buyAmount',title: '采购总数量',width: 120},
		                {field: 'buyMoney',title: '采购总价格',width: 120},
		                {field: 'buyTimes',title: '采购时间',width: 120},
		                {field:	'auName',title: '审核状态',width: 150},
		                {field:	'buyStates',title: '采购状态',width: 150},
		                {field:	'buySubmissions',title: '提交状态',width: 150},
		                {fixed: 'right',title: '操作',width: 350,align: 'center',toolbar: '#barDemo'}]
		            ],
		            done: function(res, curr, count) {
		            	windata=res.data;
		            	var tableElem = this.elem.next('.layui-table-view');
		            	
		            	$("[data-field = 'auName']").children().not(":first").each(function(index){
		            		if($(this).text()=="已审核"){
		            			//禁用
		            			tableElem.find('tr[data-index="' + index + '"]').find('button[lay-event=edit],button[lay-event=del]').addClass('layui-btn-disabled').attr('disabled',true);
		            		}
					  	});
		            	
		            	$("[data-field = 'auName']").children().not(":first").each(function(index){
		            		if($(this).text()=="未审核"){
		            			//禁用
		            			tableElem.find('tr[data-index="' + index + '"]').find('button[lay-event=confirms]').addClass('layui-btn-disabled').attr('disabled',true);
		            		}
					  	});
		            	
		            	$("[data-field = 'auName']").children().not(":first").each(function(index){
		            		if($(this).text()=="未通过"){
		            			//禁用
		            			tableElem.find('tr[data-index="' + index + '"]').find('button[lay-event=confirms]').addClass('layui-btn-disabled').attr('disabled',true);
		            		}
					  	});
		            	
		            	$("[data-field = 'buyStates']").children().not(":first").each(function(index){
		            		if($(this).text()=="已采购"){
		            			//禁用
		            			tableElem.find('tr[data-index="' + index + '"]').find('button[lay-event=confirms]').addClass('layui-btn-disabled').attr('disabled',true);
		            		}
					  	});
		            	
		            	$("[data-field = 'buyStates']").children().not(":first").each(function(index){
		            		if($(this).text()=="未采购"){
		            			//禁用
		            			tableElem.find('tr[data-index="' + index + '"]').find('button[lay-event=submission]').addClass('layui-btn-disabled').attr('disabled',true);
		            		}
					  	});
		            	
		            	$("[data-field = 'buySubmissions']").children().not(":first").each(function(index){
		            		if($(this).text()=="已提交"){
		            			//禁用
		            			tableElem.find('tr[data-index="' + index + '"]').find('button[lay-event=submission]').addClass('layui-btn-disabled').attr('disabled',true);
		            		}
					  	});
		            },
		            loading: true
		        });
		        
				//查询供应商下拉框数据
		        $.ajax({ url:"../../provider/showList.action",
                    type:'post',//method请求方式，get或者post
                    cache: false,//同步
                    dataType:'json',//预期服务器返回的数据类型
                    success:function(mes){//res为相应体,function为回调函数
                    	//循环添加框内的数据
                    	$.each(mes.data,function(index,item){
                    		$("#biaodan").append("<option value="+item.provId+">"+item.provName+"</option>");
                    	});
                    	//渲染框内的下拉框
                    	form.render();
                    }
                 });
				
				
		        renderDate('#buyTime');
		        
		        
		      	//监听搜索表单提交
		        form.on('select(biaodan)', function(data){
				  	//带条件查询
		            tableIns.reload({
		                where: {"buyCompany":data.value},
		                page: { curr: 1 }
		            });
				})
		        
		      
				$("#search").on('click',function(){
					tableIns.reload({
						where:{buyTimes:$("#buyTime").val()},
						page: { curr: 1 }
					});
				})
		        
		        
		      
				//监听工具条按钮点击
		        table.on('tool(demo)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
		            var data = obj.data; //获得当前行数据
		            var layEvent = obj.event; //获得 lay-event 对应的值
		            var tr = obj.tr; //获得当前行 tr 的DOM对象
		
		            if (layEvent === 'detail') { //采购明细
			            var  buyId=data.buyId;
	                	var index = layer.open({
	          				  type: 2,
	          				  title: '采购明细',
	          				  content: '../buy/detail.html?id='+buyId,
	          				  area: ['800px', '430px'],
	          				  maxmin: true
	          				});
	          			
		            } else if (layEvent === 'del') { //删除
		                layer.confirm('真的删除行么', function(index) {
		                	layer.close(index);
		                    update("../../buyht/delete.action",{"buyId":data.buyId,"buyType":"2"});
		                });
		            } else if (layEvent === 'edit') { //编辑
			            var  buyId=data.buyId;
	                	var index = layer.open({
	          				  type: 2,
	          				  title: '修改页面',
	          				  content: '../buy/buyaddls2.html?ids='+buyId,
	          				  area: ['800px', '30px']
	          				  //maxmin: true
	          				});
	          			layer.full(index);
		            }else if (layEvent === 'confirms') { //确认采购
		                layer.confirm('确认采购?', function(index) {
		                	update("../../buyht/cgUpdate.action",{"buyId":data.buyId,"buyState":1});
		                });
		            }else if (layEvent === 'submission') { //提交质检
		                layer.confirm('确认提交?', function(index) {
		                	layer.close(index);
		                	var d = {
				                	rowdata: data
				                };
		                	update("../../buyht/addQc.action",{"buyId":data.buyId,"buySubmission":1});
		                });
		            }
		        });
		        
	        	
		        //表单渲染
		        form.render(null, 'kit-search-form');
		        //更多筛选点击事件
		        $('#kit-search-more').on('click', function() {
		            $('.kit-search-mored').toggle();
		        });
		        var editIndex;
		        
		    });
			
			
		  //窗口自适应
			function autoHeight() {
				var h = document.body.clientHeight && document.documentElement.clientHeight - 50; //窗口高度
				//监听改变窗口大小
				window.onresize = function() {
					h = document.body.clientHeight && document.documentElement.clientHeight - 50; //窗口高度
					tableIns.reload({
						height: h
					});
				}
				return h;
			}
			
			
		  	//全屏弹窗
			function maxlayer(){
				var index = layer.open({
				  type: 2,
				  title: '药品配置',
				  content: 'https://www.baidu.com',
				  area: ['800px', '430px']
				  //maxmin: true
				});
				layer.full(index);
			}
		  	
		  	
			//增加、修改、删除、批量删除 都是用的这一个方法
		    function update(url,data){
		    	$.ajax({
		    		url: url,
                    type:'post',//method请求方式，get或者post
                    cache: false,//同步
                    dataType:'json',//预期服务器返回的数据类型
                    data: JSON.stringify(data),//表格数据序列化
                    contentType: "application/json; charset=utf-8",
                    success:function(mes){//res为相应体,function为回调函数
                    	$(".layui-laypage-btn")[0].click();//当前表格刷新
                        if(mes.status==1){ 
                        	var close = true;//有弹出层时是关闭弹出层 false不关闭
	                        $("#res").click();//调用重置按钮将表单数据清空
                        }
                        layer.msg(mes.msg);
                    },
                   	error:function(){layer.alert('数据异常，请稍后重试！！！',{icon:5});}
                 });
		    }
		  	
		  	
			
		  //日期下拉框渲染
		 	function renderDate(dateId){
		 		layui.laydate.render({
	        	  elem: dateId
	        	  ,type: 'datetime'
	        	  ,calendar: true
        	      ,range: '到'
        	      ,format: 'yyyy-M-d'
	        	});
		 	}
			
			
		  	//重新渲染表单
		  	function renderForm(){ 
	  			layui.use('form', function(){ 
		  			var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加() 
		  			form.render(); 
	  			}); 
	  		}
		    //ajax加载动画
		    $(function () {
	            $.ajaxSetup({
	                layerIndex:-1,
	                beforeSend: function () {
	                    this.layerIndex = layer.load(1, { shade: [0.8, '#ffffff'] });
	                },
	                complete: function () {
	                    layer.close(this.layerIndex);
	                },
	                error: function () {
	                    layer.alert('部分数据加载失败，可能会导致页面显示异常，请刷新后重试', {
	                        skin: 'layui-layer-molv'
	                       , closeBtn: 0
	                       , shift: 4 //动画类型
	                    });
	                }
	            });
	        });
		</script>
		<script>
			$(function(){
				$("#add").click(function(){
					location.href="buyaddls.html";
				})
			})
		</script>
		
		
		
	</body>
</html>
